<template>
  <div>
    <tablePaging :tableItem="tableItem" :operationItem="operationItem" />
    <fileImport v-model:importVisible="importVisible" :importItem="importItem" @onSuccess="onSuccess" @onError="onError" />
  </div>
</template>
<script setup>
import tablePaging from '@/views/common/component/table-paging'
import fileImport from '@/views/common/component/fileImport'
import { onMounted, reactive, ref } from 'vue'
import {
  tableItem,
  operationItem
} from './const.js'
import { ElMessage } from 'element-plus'
import { useRouter } from 'vue-router'
import {listDictItemByCode} from '@/api/config/dic'
import {delTeacher} from '@/api/teacher'
const { proxy } = getCurrentInstance();
const router = useRouter()
const importVisible = ref(false)
const importItem = reactive({
  downUrl: '/admin/teacher/download',//文件下载地址
  upUrl: '/admin/teacher/import',//文件上传地址
  blob: true,//二进制文件下载
  fileDown() { },//自定义文件下载方法 如果传入下载地址会失效 如果都不传会返回一个fileDown函数 自己@fileDown接收
  downTitle: '',//下载标题 默认1、模板下载
  downButtonText: '',//下载按钮 下载
  upButtonText: '',//上传按钮 上传
  upTitle: '',//上传标题 默认2、表格上传
  downName: '老师模版'
})
const onSuccess = (file) => {
  tableItem.pageInfo.paging()
}
const onError = (file) => { }
function importHandle() {
  importVisible.value = true
}
function exportHandle() {
  proxy.download("/admin/teacher/export", tableItem.pageInfo.request, `老师列表.xlsx`);
}
function initTableItem() {
  let menuArray = operationItem.menuArray
  let addMenu = menuArray.find(item => item.code === 'add')
  addMenu.handle = goAdd

  let delMenu = menuArray.find(item => item.code === 'del')
  delMenu.handle = delBatch

  let importMenu = menuArray.find(item => item.code === 'import')
  importMenu.handle = importHandle

  let exportMenu = menuArray.find(item => item.code === 'export')
  exportMenu.handle = exportHandle

  let btnArray = tableItem.btnArray
  let detailBtn = btnArray.find(item => item.code === 'detail')
  detailBtn.handle = goDetail

  let editBtn = btnArray.find(item => item.code === 'edit')
  editBtn.handle = goEdit
}
function goDetail(row) {
  router.push({
    path: '/modules/course/teacher/detail',
    query: { id: row.id}
  })
}
function goAdd() {
  router.push({
    path: '/modules/course/teacher/save',
    query: { type: 'add' }
  })
}
function goEdit(row) {
  router.push({
    path: '/modules/course/teacher/save',
    query: { id: row.id, type: 'edit' }
  })
}
const selectedRow = ref([])
tableItem.selectChange = (row) => {
  //构建需要删除的id列表
  selectedRow.value = row.map( (item) => {
    return item.id
  } )
}
//批量删除
function delBatch() {
  if(selectedRow.value.length < 1) {return ElMessage.warning('请先选择老师');}
  proxy.$modal.confirm('确认要删除选中的老师吗?').then(function () {
    return delTeacher(selectedRow.value);
  }).then(res => {
    tableItem.pageInfo.paging()
    ElMessage.success(res.msg);
  }).catch(() => { });
}
const initOptions = () => {
  let searchItemArray = operationItem.SearchItem
  let courseTypeItem = searchItemArray.find(item => item.code == 'teacherScopeId')
  listDictItemByCode({typeCode: 'courseTypeCode'}).then(res => {
    courseTypeItem.options = res.rows
  })
}

onMounted(() => {
  initTableItem()
  initOptions()
})
</script>

<style></style>
